<?php
/*
 * Plugin Name: NestedTags
 * Plugin URI: -----
 * Description: This plugin saves the last category then 'ands' it with the next tag selected. 
 * the category is cleared when at the home page or in the admin section.
 *
 * Version: 1.0
 * Author: Mark Tilly
 * Author URL: http://www.memeposts.com
 * License: GPLv2 or later
/*
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

add_filter( 'request', 'mct_nt_chgquery' );
function mct_nt_chgquery( $request ) {
    $dummy_query = new WP_Query();  // the query isn't run if we don't pass any query vars
    $dummy_query->parse_query( $request );  //from wordpress codex/scribu

    // Use option to save last query if a category, restore it if a tag
    if ( $dummy_query->is_home() || is_admin()){
        update_option('mct_nt_lastq','');
        return $request;
    }
    if ($dummy_query->is_category()){
        update_option('mct_nt_lastq',$request['category_name']);
        return $request;
    }
    if  ($dummy_query->is_tag()){ 
        $last_opt = get_option('mct_nt_lastq');
        $request['category_name'] = $last_opt;
    }

    return $request;
}

